package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.ProductCenter;
import com.ruoyi.system.domain.vo.ProductCenterVo;
import com.ruoyi.system.mapper.ProductCenterMapper;
import com.ruoyi.system.service.ProductCenterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProductCenterServiceImpl implements ProductCenterService {
    @Autowired
    private ProductCenterMapper productCenterMapper;

    /**
     * 研发中心产品展示的查询接口
     * @return
     */
    @Override
    public Map<String,Object> getproductCenterMap() {
        Map map=new HashMap<>();
        // 数据库所有数据
        List<ProductCenter> productCenterList= productCenterMapper.findAll();
        //数据脱敏，转化为VO
        List<ProductCenterVo> collect = productCenterList.stream()
                .map(productCenter -> new ProductCenterVo(
                        productCenter.getProductId(),
                        productCenter.getProductName(),
                        productCenter.getProductType(),
                        productCenter.getPictureUrl(),
                        productCenter.getVideoUrl(),
                        productCenter.getProductDetails(),
                        productCenter.getTop(),
                        productCenter.getProductTime(),
                        productCenter.getProductBg(),
                        productCenter.getGiteeUrl()))  // 需要确保这个字段也包含在构造函数中
                .collect(Collectors.toList());


        // 排序逻辑：先按照top字段是否为空排序，接着按top值排序
        List<ProductCenterVo> sortedProducts = collect.stream()
                .sorted(Comparator.comparing(ProductCenterVo::getTop, Comparator.nullsLast(Comparator.naturalOrder())))
                .collect(Collectors.toList());

        map.put("total",sortedProducts.size());
        map.put("record",sortedProducts);

        return map;
    }
    /**
     * 研发中心产品搜索
     * @return
     */
    @Override
    public Map<String, Object> selectproductCenterMap(String searchWord) {
        Map map=new HashMap<>();
        // 数据库所有数据
        List<ProductCenter> productCenterList= productCenterMapper.findPrdByName(searchWord);
        //数据脱敏，转化为VO
        List<ProductCenterVo> collect = productCenterList.stream()
                .map(productCenter -> new ProductCenterVo(
                        productCenter.getProductId(),
                        productCenter.getProductName(),
                        productCenter.getProductType(),
                        productCenter.getPictureUrl(),
                        productCenter.getVideoUrl(),
                        productCenter.getProductDetails(),
                        productCenter.getTop(),
                        productCenter.getProductTime(),
                        productCenter.getProductBg(),
                        productCenter.getGiteeUrl()))  // 需要确保这个字段也包含在构造函数中
                .collect(Collectors.toList());
        // 排序逻辑：先按照top字段是否为空排序，接着按top值排序
        List<ProductCenterVo> sortedProducts = collect.stream()
                .sorted(Comparator.comparing(ProductCenterVo::getTop, Comparator.nullsLast(Comparator.naturalOrder())))
                .collect(Collectors.toList());
        map.put("total",sortedProducts.size());
        map.put("record",sortedProducts);

        return map;
    }
    /**
     * 获取产品详细信息
     *
     * @return
     */
    @Override
    public ProductCenterVo findPrdById(String id) {
        // 数据库所有数据
        ProductCenter productCenter= productCenterMapper.findPrdById(id);
        ProductCenterVo productCenterVo=ProductCenterVo.builder()
                .productId(productCenter.getProductId())
                .productName(productCenter.getProductName())
                .productType(productCenter.getProductType())
                .pictureUrl(productCenter.getPictureUrl())
                .videoUrl(productCenter.getVideoUrl())
                .productDetails(productCenter.getProductDetails())
                .top(productCenter.getTop())
                .productTime(productCenter.getProductTime())
                .productBg(productCenter.getProductBg())
                .giteeUrl(productCenter.getGiteeUrl())
                .build();
        return productCenterVo;
    }
}
